package wordcount;

import java.io.*;
import java.util.*;

public class wordCount {
//    public static Map<Integer,String> map = new TreeMap<>(new Comparator<Integer>() {
//            @Override
//            public int compare(Integer o1, Integer o2) {
//                if(o1.compareTo(o2) > 0){
//                    return -1;
//                }else{
//                    return 1;
//                }
//            }
//        });
    public static Map<String,Integer> map = new HashMap<>();

    public static void countWord(){
        File filein = new File("C:"               + File.separator +
                "Users"                  + File.separator +
                "Curtain"                + File.separator +
                "Desktop"                 + File.separator +
                "大二下专业课"             + File.separator +
                "JAVA进阶"                + File.separator +
                "实验"                    + File.separator +
                "3018216226_姜天杰_实验二" + File.separator +
                "了不起的盖茨比英文.txt");
        File fileout = new File("C:"               + File.separator +
                "Users"                  + File.separator +
                "Curtain"                + File.separator +
                "Desktop"                 + File.separator +
                "大二下专业课"             + File.separator +
                "JAVA进阶"                + File.separator +
                "实验"                    + File.separator +
                "3018216226_姜天杰_实验二" + File.separator +
                "output.txt");

        try{
            if(!fileout.exists()){
                fileout.createNewFile();
            }
        }catch (IOException e){
            e.printStackTrace();
        }
        try{
            Scanner in = new Scanner(new FileInputStream(filein));
            PrintStream out = new PrintStream(new FileOutputStream(fileout));
            while(in.hasNext()){
                String line = in.next();
                //System.out.println(line);
                String[] words = line.split(" ");
                for(String word:words){
                    if(map.containsKey(word)){
                        int num = map.get(word);
                        num++;
                        map.put(word,num);
                    }else{
                        map.put(word,1);
                    }
                }
//                for(String word:words){
//                    if(map.containsValue(word)){
//                        Iterator<Map.Entry<Integer,String>> it = map.entrySet().iterator();
//                        while (it.hasNext()){
//                            if(it.next().getValue().equals(word)){
//                                System.out.println("shifoujinqu");
//                                int key = it.next().getKey();
//                                key++;
//                                System.out.println(word + " " + key);
//                                map.put(key,word);
//                            }
//                        }
//                    }else{
//                        map.put(1,word);
//                    }
//                }
            }
            List<Map.Entry<String,Integer>> lists = new ArrayList<>(map.entrySet());
            Collections.sort(lists, new Comparator<Map.Entry<String, Integer>>() {
                @Override
                public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                    return o2.getValue().compareTo(o1.getValue());
                }
            });
            for(int i = 0;i < lists.size();i++){
//                System.out.println(lists.get(i).getKey() + " " + lists.get(i).getValue());
                out.println(lists.get(i).getKey() + " " + lists.get(i).getValue() + "\t");
            }
//            for(Map.Entry<String,Integer> entry:map.entrySet()){
//                out.println(entry.getValue() + " " + entry.getKey() + "\t");
//            }

        }catch (FileNotFoundException e){
            e.printStackTrace();
        }


    }

    public static void main(String[] args) {
        countWord();
    }


}
